/**
 * Copyright 1996 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 *
 * Note, this is a very old runtime library that I have evolved since 1996. I rarely use it
 * now because the Apache Commons project has made it obsolete.
 */
package self.math.geom;

import java.awt.*;
import self.math.*;

final public class RectangleUtils {
  static public void updateRectangleToTlbr( Rectangle r, int x1, int y1, int x2, int y2 ) {
    r.width  = Math.abs( x2 - x1 );
    r.height = Math.abs( y2 - y1 );
    r.x = Math.min( x1, x2 );
    r.y = Math.min( y1, y2 );
  } 
  
  static public void getClockwiseCorners( Rectangle r, double[] xyPoints ) {
    xyPoints[0] = r.x;
    xyPoints[1] = r.y;
    xyPoints[2] = r.x + r.width;
    xyPoints[3] = r.y;
    xyPoints[4] = r.x + r.width;
    xyPoints[5] = r.y + r.height;
    xyPoints[6] = r.x;
    xyPoints[7] = r.y + r.height;
  }
  
  static public boolean isWithinOrOn( Rectangle r, double x, double y) {
      return (x >= r.x &&
              y >= r.y &&
              x <= r.x + r.width &&
              y <= r.y + r.height);
  }
  
  static public boolean getRectangleLineIntersections( Rectangle r, LinearEquation line, double[] xyPoints ) {
    boolean intersects = false;
    LinearEquation rectSide = new LinearEquation();    
    double[] corners = new double[8];
    getClockwiseCorners( r, corners );
    int noOfIntersections = 0;
    double[] xyInts = new double[2];
    for ( int c = 0; (noOfIntersections < 2) && (c < 4); c++ ) {
      int o = c + 1;
      if ( c == 3 )
        o = 0;
      rectSide.setBasedOnPoints( corners[c*2], corners[c*2+1], corners[o*2], corners[o*2+1] );
      if ( rectSide.getIntersectingPoints(line, xyInts) ) {
        if ( !isWithinOrOn(r, xyInts[0], xyInts[1]) )
          continue;
        intersects = true;
        xyPoints[noOfIntersections*2]   = xyInts[0];
        xyPoints[noOfIntersections*2+1] = xyInts[1];
        noOfIntersections++;
      }
    }
    return intersects;
  }
  
  static public void getRectCentrePointIntersections( Rectangle r, double x, double y, double[] xyPoints ) {
    LinearEquation fromToCentre = new LinearEquation( x, y, 
                                                      ((r.width/2) + r.x), 
                                                      ((r.height/2) + r.y) );
    double[] xyPointsSetOf2 = new double[4];          
    getRectangleLineIntersections( r, fromToCentre, xyPointsSetOf2 );                                            
    double dist = PointUtils.getDistance( x, y, xyPointsSetOf2[0], xyPointsSetOf2[1] );
    int set = 0;
    if ( dist > PointUtils.getDistance(x, y, xyPointsSetOf2[2], xyPointsSetOf2[3]) )
      set = 2;
    xyPoints[0] = xyPointsSetOf2[set];
    xyPoints[1] = xyPointsSetOf2[set+1];
  }  
}